package com.nulstudio.cloud.supplier.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nulstudio.cloud.common.entity.NulSupplier;
import com.nulstudio.cloud.common.exception.NulException;
import com.nulstudio.cloud.common.exception.NulExceptionConstants;
import com.nulstudio.cloud.supplier.domain.dto.SupplierDto;
import com.nulstudio.cloud.common.domain.vo.SupplierVo;
import com.nulstudio.cloud.supplier.mapper.SupplierMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public final class SupplierService {

    private final Logger log = LoggerFactory.getLogger(SupplierService.class);

    @Resource
    @NonNull
    private SupplierMapper supplierMapper;

    public PageInfo<SupplierVo> fetchAll(@NonNull String name, int page, int pageSize) {
        final PageInfo<SupplierVo> result;
        try (
                final Page<NulSupplier> ignored = PageHelper.startPage(page, pageSize);
        ) {
            final List<NulSupplier> suppliers = supplierMapper.fetchAll(name);
            final PageInfo<NulSupplier> supplierPageInfo = new PageInfo<>(suppliers);
            result = new PageInfo<>(suppliers.stream().map(SupplierVo::new).collect(Collectors.toList()));
            result.setTotal(supplierPageInfo.getTotal());
        } catch (Exception exception) {
            log.error("Error", exception);
            throw new NulException(NulExceptionConstants.INTERNAL_SERVER_ERROR, exception.getMessage());
        }
        return result;
    }

    @NonNull
    public SupplierVo getSupplierById(@NonNull int id) {
        final SupplierVo result = supplierMapper.findSupplierById(id);
        if (result == null) {
            throw new NulException(NulExceptionConstants.SUPPLIER_NOT_EXIST);
        }
        return result;
    }

    public void create(@NonNull SupplierDto supplierDto) {
        final NulSupplier supplier = new NulSupplier(supplierDto.name(), supplierDto.contact(), supplierDto.phone(),
                supplierDto.email(), supplierDto.address());
        NulSupplier.assertValidity(supplier);
        final int result = supplierMapper.insert(supplier);
        if (result <= 0) {
            throw new NulException(NulExceptionConstants.INTERNAL_SERVER_ERROR);
        }
    }

    public void update(int id, @NonNull SupplierDto supplierDto) {
        final NulSupplier supplier = new NulSupplier(id, supplierDto.name(), supplierDto.contact(), supplierDto.phone(),
                supplierDto.email(), supplierDto.address());
        NulSupplier.assertValidity(supplier);
        final int result = supplierMapper.update(supplier);
        if (result <= 0) {
            throw new NulException(NulExceptionConstants.SUPPLIER_NOT_EXIST);
        }
    }

    public void delete(int id) {
        this.delete(List.of(id));
    }

    public void delete(@NonNull List<Integer> suppliers) {
        final int result = supplierMapper.delete(suppliers);
        if (result <= 0) {
            throw new NulException(NulExceptionConstants.SUPPLIER_NOT_EXIST);
        }
    }
}
